package ${package.Controller};


import cn.hutool.core.collection.CollectionUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.gsy.aop.annotation.ParameterValid;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.response.BaseResponse;
import org.springframework.beans.BeanUtils;
import com.gsy.request.IdRequest;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};

<#if restControllerStyle>

<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping(value = "/${cfg.modeName}/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${cfg.lowerName}</#if>", consumes = "application/json", produces = "application/json")
@Api(value = "${table.comment!}controller", tags = {"${table.name} 操作接口"})
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    private final static Logger LOGGER = LoggerFactory.getLogger(${table.controllerName}.class);
   
    @Resource
    private ${table.serviceName} ${cfg.lowerName}Service;
    
    /**
    * 更新${table.comment!}
    * @param request
    * @return
    */
    @ApiOperation(value = "更新${table.name}信息", notes = "根据请求参数信息更新${table.name}")
    @PutMapping("${cfg.lowerName}")
    @ParameterValid(apiName = "更新${table.name}信息", formatObj = Update${cfg.className}Request.class)
    public BaseResponse update${cfg.className}Information(@RequestBody Update${cfg.className}Request request) {
    
        ${cfg.className}Entity entity = ${cfg.lowerName}Service.getById(request.getId());
        if(entity == null){
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "不存在该条信息！");
        }
        BeanUtils.copyProperties(request, entity);

        Boolean flag = ${cfg.lowerName}Service.updateById(entity);
        if (flag) {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode());
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "更新失败！");
        }
    }
    
    
    /**
    * 删除${table.name}信息
    * @param request
    * @return
    */
    @ApiOperation(value = "删除${table.name}信息", notes = "根据请求参数信息删除${table.name}信息")
    @DeleteMapping("${cfg.lowerName}/{id}")
    public BaseResponse delete${cfg.className}(@PathVariable Integer id) {
    ${cfg.className}Entity entity = ${cfg.lowerName}Service.getById(request.getId());
    
        if (entity == null) {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "不存在该条信息！");
        }

        ${cfg.lowerName}Service.removeById(request.getId());
        return new BaseResponse(SystemStatusEnum.SUCCESS.getCode());

    }
    
    
    /**
    * 新增${table.comment!}
    * @param request
    * @return
    */
    @ApiOperation(value = "新增${table.name}信息", notes = "根据请求参数信息新增${table.name}")
    @PostMapping("${cfg.lowerName}")
    @ParameterValid(apiName = "新增${table.name}信息", formatObj = Add${cfg.className}Request.class)
    @SentinelResource(value = "/${cfg.modeName}/${cfg.lowerName}/add")
    public BaseResponse add${cfg.className}(@RequestBody Add${cfg.className}Request request) {
    
        ${cfg.className}Entity entity = new ${cfg.className}Entity();
        BeanUtils.copyProperties(request, entity);

        Boolean flag = ${cfg.lowerName}Service.save(entity);
        if (flag) {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode());
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "新增失败！");
        }
    }
    
    
    /**
    * 获取${table.comment!}列表
    * @param request
    * @return
    */
    @ApiOperation(value = "获取${table.name}列表", notes = "根据请求参数搜索获取${table.name}列表")
    @GetMapping("${cfg.lowerName}")
    @ParameterValid(apiName = "获取${table.name}列表", formatObj = Get${cfg.className}ListRequest.class)
    @SentinelResource(value = "/${cfg.modeName}/${cfg.lowerName}/list")
    public BaseResponse get${cfg.className}List(@RequestBody Get${cfg.className}ListRequest request) {
    
        List<${cfg.className}Entity> entities = ${cfg.lowerName}Service.list();

        if (CollectionUtil.isNotEmpty(entities)) {
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS.getCode());
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.SUCCESS.getCode(), "获取数据为空！");
        }
     }

}
</#if>
